package com.qfedu.controller;

import com.alibaba.fastjson.JSONObject;
import com.qfedu.entity.Song;
import com.qfedu.entity.SongList;
import com.qfedu.service.SongListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @author zhang
 * @Title: commonValidateAdd
 * @date : 2022/2/22 19:36
 */
@Controller
public class SongListController {

    @Autowired
    private SongListService songListService;

    @RequestMapping("/songList")
    @ResponseBody
    public List<SongList> findAllSongList(){
        return songListService.findAllSongList();
    }

    @RequestMapping("/songList/add")
    @ResponseBody
    public JSONObject addSongList(SongList songList){
        int i = songListService.addSongList(songList);
        JSONObject jsonObject = new JSONObject();
        if (i > 0){
            jsonObject.put("code",1);
            jsonObject.put("Msg","添加成功");
        }else {
            jsonObject.put("code",0);
            jsonObject.put("Msg","添加失败");
        }
        return jsonObject;
    }

    @RequestMapping("/songList/update")
    @ResponseBody
    public JSONObject updateSongList(SongList songList){
        int i = songListService.updateSongList(songList);
        JSONObject jsonObject = new JSONObject();
        if (i > 0){
            jsonObject.put("code",1);
            jsonObject.put("Msg","更新成功");
        }else {
            jsonObject.put("code",0);
            jsonObject.put("Msg","更新失败");
        }
        return jsonObject;
    }

    @RequestMapping("/songList/delete")
    @ResponseBody
    public JSONObject deleteSongList(SongList songList){
        int i = songListService.deleteSongList(songList);
        JSONObject jsonObject = new JSONObject();
        if (i > 0){
            jsonObject.put("code",1);
            jsonObject.put("Msg","删除成功");
        }else {
            jsonObject.put("code",0);
            jsonObject.put("Msg","删除失败");
        }
        return jsonObject;
    }

    @RequestMapping("/songList/img/update")
    @ResponseBody
    public JSONObject updateSongListById(MultipartFile file, SongList songList) throws IOException {
        String originalFilename = file.getOriginalFilename();
        String url = "/img/songListPic/" + originalFilename;//originalFilename 文件名
        originalFilename = "D:\\qianfeng\\software\\IDEA\\project3-background\\project3background\\img\\songListPic\\" + originalFilename;
        System.out.println("originalFilename");

        //文件上传
        File file1 = new File(originalFilename);
        //将接收到的文件转移到新的文件下
        file.transferTo(file1);


        songList.setPic(url);
        JSONObject jsonObject = songListService.updateSongListById(songList);
        jsonObject.put("pic",url);
        return jsonObject;

    }


}
